using System;

using octalforty.Waffle.ComponentModel.Managers;

using octalforty.Waffle.ObjectModel.Business.Objects;

using octalforty.Waffle.Web.Engine.Globalization.Formatters;
using octalforty.Waffle.Web.Engine.Web.UI;

using Spring.DataBinding;
using Spring.Transaction;

using Spring.Transaction.Interceptor;

namespace octalforty.Waffle.Web.UI.Views.Signup
{
    /// <summary>
    /// Signup page.
    /// </summary>
    public partial class Default : Page, IUserManagerServiceDependency
    {
        #region Private Member Variables
        private User user;
        private IUserManagerService userManagerService;
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets or sets a reference to the signing up user.
        /// </summary>
        public new User User
        {
            get { return user; }
            set { user = value; }
        }
        #endregion

        /// <summary>
        /// Initializes a new instance of <see cref="Default"/> class.
        /// </summary>
        public Default()
        {
        }

        #region Page Members
        /// <summary>
        /// Initializes dialog result and unbinds data from the controls
        /// into a data model.
        /// </summary>
        /// <param name="e">Event arguments.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            signupForm.Signup += new EventHandler(signupForm_Signup);
        }

        /// <summary>
        /// Initializes the model when the page is first requested and on all subsequent postbacks.
        /// </summary>
        protected override void InitializeModelEx()
        {
            base.InitializeModelEx();

            User = new User();
        }

        /// <summary>
        /// Initializes the data bindings.
        /// </summary>
        protected override void InitializeDataBindings()
        {
            base.InitializeDataBindings();

            BindingManager.AddBinding("signupForm.FirstName", "User.FirstName", BindingDirection.Bidirectional);
            BindingManager.AddBinding("signupForm.LastName", "User.LastName", BindingDirection.Bidirectional);
            BindingManager.AddBinding("signupForm.EmailAddress", "User.EmailAddress", BindingDirection.Bidirectional);
            BindingManager.AddBinding("signupForm.Login", "User.Login", BindingDirection.Bidirectional);
            BindingManager.AddBinding("signupForm.Password", "User.Password", BindingDirection.SourceToTarget,
                                      new PasswordFormatter(SecurityService));
        }
        #endregion

        #region IUserManagerServiceDependency Members
        /// <summary>
        /// Gets or sets a reference to the <see cref="IUserManagerService" />.
        /// </summary>
        public IUserManagerService UserManagerService
        {
            get { return userManagerService; }
            set { userManagerService = value; }
        }
        #endregion

        [Transaction(TransactionPropagation.Required)]
        private void signupForm_Signup(object sender, EventArgs e)
        {
            UserManagerService.SaveUser(User);
            Response.Redirect("~/", true);
        }
    }
}
